/*
* Copyright (C) 2004-2006  Autodesk, Inc.
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of version 2.1 of the GNU Lesser
* General Public License as published by the Free Software Foundation.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
*/

#include "stdafx.h"
#include "FdoCommonOSUtil.h"
#include "FdoWcsCoverageSummary.h"
#include "FdoWcsCoverageSummaryCollection.h"
#include "FdoWcsXmlGlobals.h"
#include "FdoOwsWGS84BoundingBox.h"


FdoWcsCoverageSummary::FdoWcsCoverageSummary(void) :
mParent(NULL)
{
    mKeywordList = FdoStringCollection::Create();
    mWGS84BoundingBoxes = FdoOwsBoundingBoxCollection::Create();
    mBoundingBoxes = FdoOwsBoundingBoxCollection::Create();
    mSupportedCRSes = FdoStringCollection::Create();
    mSupportedFormats = FdoStringCollection::Create();
    mChildCoverageSummaries = FdoWcsCoverageSummaryCollection::Create();
}

FdoWcsCoverageSummary::~FdoWcsCoverageSummary(void)
{
}

FdoWcsCoverageSummary* FdoWcsCoverageSummary::Create()
{
    return new FdoWcsCoverageSummary();
}

void FdoWcsCoverageSummary::Dispose()
{
    delete this;
}

FdoXmlSaxHandler* FdoWcsCoverageSummary::XmlStartElement(FdoXmlSaxContext *context, FdoString *uri, FdoString *name, FdoString *qname, FdoXmlAttributeCollection *atts)
{
    FdoXmlSaxHandler* pRet = NULL;

    try
    {
        VALIDATE_ARGUMENT(context);
        VALIDATE_ARGUMENT(name);

        pRet = BaseType::XmlStartElement(context, uri, name, qname, atts);
        if (pRet == NULL)
        {
            if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesCoverageTitle) == 0 ||
                FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesCoverageAbstract) == 0 ||
                FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesCoverageIdentifier) == 0 ||
                FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesCoverageKeyword) == 0 ||
                FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesSupportedCRS) == 0 ||
                FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesSupportedFormat) == 0)
            {
                mXmlContentHandler = FdoXmlCharDataHandler::Create();
                pRet = mXmlContentHandler;
            }
            else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesWGS84BoundingBox) == 0)
            {
                FdoOwsBoundingBoxP wgs84BoundingBox = FdoOwsWGS84BoundingBox::Create();
                
                FdoOwsBoundingBoxCollectionP wgs84BoundingBoxes = GetWGS84BoundingBoxCollection();
                wgs84BoundingBoxes->Add(wgs84BoundingBox);

                // Add the WGS84 bounding box to the collection.
                FdoOwsBoundingBoxCollectionP boundingBoxes = GetBoundingBoxCollection();
                boundingBoxes->Add(wgs84BoundingBox);

                pRet = wgs84BoundingBox;
            }
            else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesBoundingBox) == 0)
            {
                FdoOwsBoundingBoxP boundingBox = FdoOwsBoundingBox::Create();
                boundingBox->InitFromXml(context, atts);

                FdoOwsBoundingBoxCollectionP boundingBoxes = GetBoundingBoxCollection();
                boundingBoxes->Add(boundingBox);

                pRet = boundingBox;
            }
            else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesCoverageSummary) == 0)
            {
                FdoWcsCoverageSummaryP coverageSummary = FdoWcsCoverageSummary::Create();

                FdoWcsCoverageSummaryCollectionP coverageSummaries = GetCoverageSummaries();
                coverageSummaries->Add(coverageSummary);
                coverageSummary->SetParent(this);

                pRet = coverageSummary;
            }
        }
    }
    catch (FdoException* e)
    {
        context->AddError(e);
        e->Release();
    }

    return pRet;
}

FdoBoolean FdoWcsCoverageSummary::XmlEndElement(FdoXmlSaxContext *context, FdoString *uri, FdoString *name, FdoString *qname)
{
    try
    {
        VALIDATE_ARGUMENT(context);
        VALIDATE_ARGUMENT(name);

        if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesCoverageTitle) == 0)
            SetTitle(mXmlContentHandler->GetString());
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesCoverageAbstract) == 0)
            SetAbstract(mXmlContentHandler->GetString());
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesCoverageIdentifier) == 0)
            SetIdentifier(mXmlContentHandler->GetString());
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesCoverageKeyword) == 0)
        {
            FdoStringsP keywords = GetKeywordList();
            keywords->Add(mXmlContentHandler->GetString());
        }
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesSupportedCRS) == 0)
        {
            FdoStringsP supportedCRSes = GetSupportedCRSNames();
            supportedCRSes->Add(mXmlContentHandler->GetString());
        }
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsCapabilitiesSupportedFormat) == 0)
        {
            FdoStringsP supportedFormats = GetSupportedFormatNames();
            supportedFormats->Add(mXmlContentHandler->GetString());
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return BaseType::XmlEndElement(context, uri, name, qname);
}

FdoString* FdoWcsCoverageSummary::GetIdentifier() const
{
    return mIdentifier;
}

void FdoWcsCoverageSummary::SetIdentifier(FdoString* identifier)
{
    mIdentifier = identifier;
}

FdoString* FdoWcsCoverageSummary::GetTitle() const
{
    return mTitle;
}

void FdoWcsCoverageSummary::SetTitle(FdoString *title)
{
    mTitle = title;
}

FdoString* FdoWcsCoverageSummary::GetAbstract() const
{
    return mAbstract;
}

void FdoWcsCoverageSummary::SetAbstract(FdoString *abstract)
{
    mAbstract = abstract;
}

FdoStringCollection* FdoWcsCoverageSummary::GetKeywordList() const
{
    return FDO_SAFE_ADDREF(mKeywordList.p);
}

FdoOwsBoundingBoxCollection* FdoWcsCoverageSummary::GetWGS84BoundingBoxCollection() const
{
    return FDO_SAFE_ADDREF(mWGS84BoundingBoxes.p);
}

FdoOwsBoundingBoxCollection* FdoWcsCoverageSummary::GetBoundingBoxCollection() const
{
    return FDO_SAFE_ADDREF(mBoundingBoxes.p);
}

FdoStringCollection* FdoWcsCoverageSummary::GetSupportedCRSNames() const
{
    return FDO_SAFE_ADDREF(mSupportedCRSes.p);
}

FdoStringCollection* FdoWcsCoverageSummary::GetSupportedFormatNames() const
{
    return FDO_SAFE_ADDREF(mSupportedFormats.p);
}

FdoWcsCoverageSummaryCollection* FdoWcsCoverageSummary::GetCoverageSummaries() const
{
    return FDO_SAFE_ADDREF(mChildCoverageSummaries.p);
}

FdoWcsCoverageSummary* FdoWcsCoverageSummary::GetParent() const
{
    return FDO_SAFE_ADDREF(mParent);
}

void FdoWcsCoverageSummary::SetParent(FdoWcsCoverageSummary* cs)
{
    // Soft reference to avoid the circular reference.
    mParent = cs;
}

bool FdoWcsCoverageSummary::FindCoordinateReferenceSystem (FdoString* coordSystem)
{
	bool bFound = false;
	FdoStringsP crsNames = GetSupportedCRSNames ();
	if (crsNames != NULL)
	{
		bFound = (crsNames->IndexOf (coordSystem) != -1);
	}

	if (!bFound)
	{
		FdoWcsCoverageSummaryP parent = this->GetParent ();
		while (parent != NULL)
		{
			bFound = parent->FindCoordinateReferenceSystem (coordSystem);
			if (bFound)
				return bFound;

			parent = parent->GetParent ();
		}
	}

	return bFound;
}